package com.jinlia.show.common.core.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.jinlia.show.common.core.entity.TokenModer;
import com.jinlia.show.common.core.exception.MyRuntimeException;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

public class JavaJwtUtils {
    // 签名密钥
    public static final String SECRET = "!DAR$";

    public static String builderUserToken(TokenModer tokenModer) {
        // 指定token过期时间为7天
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, 3000);
        JWTCreator.Builder builder = JWT.create();
        // 构建payload
        builder.withClaim("userId",  Long.valueOf(tokenModer.getUserId()));
        builder.withClaim("userName", tokenModer.getUserName());
        // 指定过期时间和签名算法
        return builder.withExpiresAt(calendar.getTime()).sign(Algorithm.HMAC256(SECRET));
    }
    public static String builderCompanyToken(TokenModer tokenModer) {
        // 指定token过期时间为7天
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, 300000);
        JWTCreator.Builder builder = JWT.create();
        // 构建payload
        builder.withClaim("userId", Long.valueOf(tokenModer.getUserId()));
        builder.withClaim("userName", tokenModer.getUserName());
        builder.withClaim("companyId", tokenModer.getCompanyId());
        // 指定过期时间和签名算法
        return builder.withExpiresAt(calendar.getTime()).sign(Algorithm.HMAC256(SECRET));
    }
    /**
     * 刷新token
     */
    public static String refreshToken(String token) {
        DecodedJWT oleDecodedJWT = decode(token);
        JWTCreator.Builder builder = JWT.create();
        // 构建payload
        for (String s : oleDecodedJWT.getClaims().keySet()) {
            Claim claim = oleDecodedJWT.getClaim(s);
            builder.withClaim(s, claim.asString());
        }
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, 40);
        return builder.withExpiresAt(calendar.getTime()).sign(Algorithm.HMAC256(SECRET));
    }

    /**
     * 解析token
     *
     * @param token token字符串
     * @return 解析后的token
     */
    public static DecodedJWT decode(String token) {
        return verify(token);
    }

    public static Date checkIndate(String token) {
        DecodedJWT decodedJWT = verify(token);
        return decodedJWT.getExpiresAt();
    }

    private static DecodedJWT verify(String token) {
        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(SECRET)).build();
        DecodedJWT decodedJWT = null;
        try {
            decodedJWT = jwtVerifier.verify(token);
        } catch (TokenExpiredException e) {
            throw new MyRuntimeException("0", "登录过期请重新登录!!!!!");
        } catch (NullPointerException e) {
            throw new MyRuntimeException("0", "登录失效,请重新登录!!!");
        }

        return decodedJWT;
    }

    public static void main(String[] args) {
        JWTCreator.Builder builder = JWT.create();
        // 构建payload
        builder.withClaim("userId", "user.getId()");
        builder.withClaim("userName", "user.getNickName()");
        //
        String token = builder.sign(Algorithm.HMAC256(JavaJwtUtils.SECRET));
        System.out.println(token);
        DecodedJWT decode = JavaJwtUtils.decode(token);

        Date expiresAt = decode.getExpiresAt();
        System.out.println(expiresAt + ":" + expiresAt);

        System.out.println(decode.getClaim("userId").asString());
    }


}
